//
//  WelcomeViewController.swift
//  WeChat
//
//  Created by 梁迪 on 15/12/16.
//  Copyright © 2015年 梁迪. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func loadView() {
        
        view = backImageView
        
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        showAnimation()
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
    }

    
    
    private func setUpUI(){
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
           
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            
        }
        
        iconView.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage:UIImage(named: "avatar_default_big"))
        
        
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
        
    }
    
    
    private func showAnimation(){
        welcomeLabel.alpha = 0
        let offset = -UIScreen.mainScreen().bounds.height + 200
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(offset)
            
        }
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
        self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
             UIView.animateWithDuration(0.5, animations: { () -> Void in
                self.welcomeLabel.alpha = 1
                }, completion: { (_) -> Void in
                     NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
                    
             })
                
                
                
        }
        
        
        
        
    }
    
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    //懒加载
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView:UIImageView = UIImageView()
    private lazy var welcomeLabel:UILabel = UILabel(title: (UserAccountViewModel().userName ?? "") + "欢迎归来", color: UIColor.lightGrayColor(), fontSize: 16)
    
    
    
    
    
    
    
    
    
    
}
